package com.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UtilsFile {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss SSS");;
	/**
	 * 根据文件路径获得文件
	 * @param path 文件路径
	 * @param isAutoCreate  1, ture，获得不到文件后自动创建. 2,false，获得不到文件后不自动创建.
	 * @return 此文件
	 * @throws IOException
	 */
	public static File getFile(String path, boolean isAutoCreate) throws IOException{
		File file = new File(path);
		if(!file.exists()){
			file.createNewFile();
		}
		
		return file;
	}
	
	public static StringBuffer readFileByLine(String filename) throws IOException{
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		br = new BufferedReader(new FileReader(filename));
		String line ;
		while((line = br.readLine()) != null){
			sb.append(line+"<br/>");
		}
		
		br.close();
		
		return sb;
	}
	public static List<String> readFileLineToList(String file) throws IOException{
		List<String> list = new ArrayList<String>();
		BufferedReader br = null;
		br = new BufferedReader(new FileReader(file));
		String line ;
		while((line = br.readLine()) != null){
			list.add(line);
		}
		
		br.close();
		
		return list;
	}
	
	public static StringBuffer readFile(String filename) throws IOException{
		StringBuffer sb = new StringBuffer();
		FileInputStream fin = new FileInputStream(new File(filename));
		byte[] b = new byte[2024];
		int read = 0;
		while((read = fin.read(b)) != -1){
			sb.append(new String(b, 0, read));
		}
		
		fin.close();

		return sb;
	}
	
	
}
